package sedev.struts.portlet;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import sedev.shopping.business.DonViGiaService;
import sedev.shopping.entities.DonViGia;
import sedev.shopping.entities.ListDonViGia;
import sedev.shopping.util.SedevUtil;

import com.liferay.portal.util.PortalUtil;
import com.opensymphony.xwork2.ActionSupport;

public class CurrencyAction extends ActionSupport implements
		ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private Long ownerId;	
	
	private int  preExecute() {
		if(SedevUtil.isAdministrator(this.ownerId) == false){
			return -1;
		}
		return 0;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.ownerId = PortalUtil.getUserId(this.request);
	}

	@Override
	public String execute() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}		
		ListDonViGia listdata = DonViGiaService.getListDataByAll();
		this.request.setAttribute("listdonvigia", listdata);		
		return ActionSupport.SUCCESS;
	}

	public String post() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		return ActionSupport.SUCCESS;
	}

	public String edit() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		DonViGia data = new DonViGia();
		try {
			int id = Integer.valueOf((String) this.request.getParameter("currencyid"));
			data = DonViGiaService.getData(id);
			if (data != null) {
				this.request.setAttribute("donvigia", data);				
				return ActionSupport.SUCCESS;
			} else {
				this.request.setAttribute("errorCode", -1);
			}
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		// return index page
		return ActionSupport.ERROR;
	}

	public String add() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		DonViGia data = new DonViGia();
		try {
			if (this.initData(data)) {
				if (DonViGiaService.addData(data)) {
					this.request.setAttribute("errorCode", 1);
					this.request.setAttribute("donvigia", data);					
					return ActionSupport.SUCCESS;
				}
			} else {
				this.request.setAttribute("errorCode", -1);
			}
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		// return post page		
		return ActionSupport.ERROR;
	}

	public String update() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		DonViGia data = new DonViGia();
		try {
			data.setId(Integer.valueOf(this.request.getParameter("id")));
			if (data.getId() != null) {
				if (this.initData(data)) {
					if (DonViGiaService.updateData(data)) {
						this.request.setAttribute("errorCode", 2);
					} else {
						this.request.setAttribute("errorCode", -1);
					}
				} else {
					this.request.setAttribute("errorCode", -1);
				}
				this.request.setAttribute("donvigia", data);				
				return ActionSupport.SUCCESS;
			} else {
				this.request.setAttribute("errorCode", -1);
			}
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		// return edit page
		return ActionSupport.ERROR;
	}

	public String remove() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}		
		int id = Integer.valueOf(this.request.getParameter("id"));
		if(DonViGiaService.removeData(id)){
			this.request.setAttribute("errorCode", 1);
		}else{
			this.request.setAttribute("errorCode", -1);
		}
		ListDonViGia listdata = DonViGiaService.getListDataByAll();
		this.request.setAttribute("listdonvigia", listdata);
		return ActionSupport.SUCCESS;
	}

	private boolean initData(DonViGia data) {
		try {
			data.setTen(this.request.getParameter("title"));
		} catch (Exception ex) {
			data.setTen("");
		}		
		return true;
	}
}
